﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UtilZ.Dotnet.Ex.Base
{
    /// <summary>
    /// 字符串类型扩展方法类
    /// </summary>
    public static class StringEx
    {

        /// <summary>
        /// 计算字符串中包含的中文字符数
        /// </summary>
        /// <param name="str">待计算的字符串</param>
        /// <returns>字符串中包含的中文字符数</returns>
        public static int CalculateChineseCharCount(this string str)
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                return 0;
            }

            //已计算的中文字符个数
            int chineseCharCount = 0;
            //NExtendObject.DisorderLoop(str, (ch) =>
            //{
            //    if (NExtendChar.IsChineseChar(ch))
            //    {
            //        chineseCharCount++;
            //    }

            //    return true;
            //});

            for (int i = 0; i < str.Length; i++)
            {
                if (CharEx.IsChineseChar(str[i]))
                {
                    chineseCharCount++;
                }
            }

            return chineseCharCount;

            //if (str == null)
            //{
            //    throw new ArgumentNullException(NExtendObject.GetVarName(xx => str));
            //}

            ////空字符串返回0
            //if (string.IsNullOrEmpty(str))
            //{
            //    return 0;
            //}

            ////已计算的中文字符个数
            //int chineseCharCount = 0;
            //int length = str.Length / 2;
            //if (str.Length % 2 != 0)
            //{
            //    length += 1;
            //}

            //*************************************************
            // *    fp              pl      pr              rp
            // * |-->|              |<--|-->|              |<--|
            // * |______________________|______________________|
            // **************************************************/
            //int fp = 0;
            //int pl = length - 1;
            //int pr = 0;
            //int rp = str.Length - 1;
            ////fp,pl,pr,rp为四个方向移动的索引指针

            //for (fp = 0, pr = length; fp < length; fp++, pr++)
            //{
            //    if (fp <= pl && NExtendChar.IsChineseChar(str[fp]))
            //    {
            //        chineseCharCount++;
            //    }

            //    if (pl > fp)
            //    {
            //        if (NExtendChar.IsChineseChar(str[pl]))
            //        {
            //            chineseCharCount++;
            //        }

            //        pl--;
            //    }

            //    if (pr <= rp && NExtendChar.IsChineseChar(str[pr]))
            //    {
            //        chineseCharCount++;
            //    }

            //    if (rp > pr)
            //    {
            //        if (NExtendChar.IsChineseChar(str[rp]))
            //        {
            //            chineseCharCount++;
            //        }

            //        rp--;
            //    }
            //}

            //return chineseCharCount;
        }



        /// <summary>
        /// 字符串的字母是否全大写[true:全大写;false:大小写混合]
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>true:全大写;false:大小写混合</returns>
        public static bool IsAllUpper(this string str)
        {
            //bool result = true;
            //NExtendObject.DisorderLoop(str, (ch) =>
            //{
            //    if (NExtendChar.IsLetter(ch) && NExtendChar.IsLetterLower(ch))
            //    {
            //        result = false;
            //        return false;
            //    } 

            //    return true;
            //});

            //return result;

            char ch;
            for (int i = 0; i < str.Length; i++)
            {
                ch = str[i];
                if (CharEx.IsLetter(ch) && CharEx.IsLetterLower(ch))
                {
                    return false;
                }
            }

            return true;

            //if (string.IsNullOrEmpty(str))
            //{
            //    throw new ArgumentNullException(NExtendObject.GetVarName(xx => str));
            //}

            ////已计算的中文字符个数
            //bool result = true;
            //int length = str.Length / 2;
            //if (str.Length % 2 != 0)
            //{
            //    length += 1;
            //}

            //*************************************************
            // *    fp              pl      pr              rp
            // * |-->|              |<--|-->|              |<--|
            // * |______________________|______________________|
            // **************************************************/
            //int fp = 0;
            //int pl = length - 1;
            //int pr = 0;
            //int rp = str.Length - 1;

            //char ch;
            ////fp,pl,pr,rp为四个方向移动的索引指针

            //for (fp = 0, pr = length; fp < length; fp++, pr++)
            //{
            //    if (fp <= pl)
            //    {
            //        ch = str[fp];
            //        if (NExtendString.IsLetter(ch) && NExtendString.IsLetterLower(ch))
            //        {
            //            result = false;
            //            break;
            //        }
            //    }

            //    if (pl > fp)
            //    {
            //        ch = str[pl];
            //        if (NExtendString.IsLetter(ch) && NExtendString.IsLetterLower(ch))
            //        {
            //            result = false;
            //            break;
            //        }

            //        pl--;
            //    }

            //    if (pr <= rp)
            //    {
            //        ch = str[pr];
            //        if (NExtendString.IsLetter(ch) && NExtendString.IsLetterLower(ch))
            //        {
            //            result = false;
            //            break;
            //        }
            //    }

            //    if (rp > pr)
            //    {
            //        ch = str[rp];
            //        if (NExtendString.IsLetter(ch) && NExtendString.IsLetterLower(ch))
            //        {
            //            result = false;
            //            break;
            //        }

            //        rp--;
            //    }
            //}

            //return result;
        }

        /// <summary>
        /// 字符串的字母是否全小写[true:全小写;false:大小写混合]
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>true:全小写;false:大小写混合</returns>
        public static bool IsAllLower(this string str)
        {
            char ch;
            for (int i = 0; i < str.Length; i++)
            {
                ch = str[i];
                if (CharEx.IsLetter(ch) && CharEx.IsLetterUpper(ch))
                {
                    return false;
                }
            }

            return true;

            //bool result = true;
            //NExtendObject.DisorderLoop(str, (ch) =>
            //{
            //    if (NExtendChar.IsLetter(ch) && NExtendChar.IsLetterUpper(ch))
            //    {
            //        result = false;
            //        return false;
            //    }

            //    return true;
            //});

            //return result;

            //if (string.IsNullOrEmpty(str))
            //{
            //    throw new ArgumentNullException(NExtendObject.GetVarName(xx => str));
            //}

            ////已计算的中文字符个数
            //bool result = true;
            //int length = str.Length / 2;
            //if (str.Length % 2 != 0)
            //{
            //    length += 1;
            //}

            //*************************************************
            // *    fp              pl      pr              rp
            // * |-->|              |<--|-->|              |<--|
            // * |______________________|______________________|
            // **************************************************/
            //int fp = 0;
            //int pl = length - 1;
            //int pr = 0;
            //int rp = str.Length - 1;

            //char ch;
            ////fp,pl,pr,rp为四个方向移动的索引指针

            //for (fp = 0, pr = length; fp < length; fp++, pr++)
            //{
            //    if (fp <= pl)
            //    {
            //        ch = str[fp];
            //        if (NExtendString.IsLetter(ch) && NExtendString.IsLetterUpper(ch))
            //        {
            //            result = false;
            //            break;
            //        }
            //    }

            //    if (pl > fp)
            //    {
            //        ch = str[pl];
            //        if (NExtendString.IsLetter(ch) && NExtendString.IsLetterUpper(ch))
            //        {
            //            result = false;
            //            break;
            //        }

            //        pl--;
            //    }

            //    if (pr <= rp)
            //    {
            //        ch = str[pr];
            //        if (NExtendString.IsLetter(ch) && NExtendString.IsLetterUpper(ch))
            //        {
            //            result = false;
            //            break;
            //        }
            //    }

            //    if (rp > pr)
            //    {
            //        ch = str[rp];
            //        if (NExtendString.IsLetter(ch) && NExtendString.IsLetterUpper(ch))
            //        {
            //            result = false;
            //            break;
            //        }

            //        rp--;
            //    }
            //}

            //return result;
        }




        /// <summary>
        /// 从当前 System.String 对象移除所有前导空白字符和尾部空白字符
        /// </summary>
        /// <param name="obj">转换为string进行Trim操作的对象</param>
        /// <param name="trimChars">要删除的 Unicode 字符的数组，或 null</param>
        /// <returns>从当前字符串的开头和结尾删除所有空白字符后剩余的字符串</returns>
        public static string Trim(object obj, params char[] trimChars)
        {
            if (obj == null)
            {
                return string.Empty;
            }

            if (obj is string)
            {
                return ((string)obj).Trim(trimChars);
            }
            else
            {
                return obj.ToString().Trim(trimChars);
            }
        }

        /// <summary>
        /// 从当前 System.String 对象移除数组中指定的一组字符的所有前导匹配项
        /// </summary>
        /// <param name="trimChars">要删除的 Unicode 字符的数组，或 null</param>
        /// <param name="obj">转换为string进行TrimStart操作的对象</param>
        /// <returns>从当前字符串的结尾移除所出现的所有 trimChars 参数中的字符后剩余的字符串。如果 trimChars 为 null 或空数组，则改为删除 Unicode空白字符</returns>
        public static string TrimStart(object obj, params char[] trimChars)
        {
            if (obj == null)
            {
                return string.Empty;
            }

            if (obj is string)
            {
                return ((string)obj).TrimStart(trimChars);
            }
            else
            {
                return obj.ToString().TrimStart(trimChars);
            }
        }

        /// <summary>
        /// 从当前 System.String 对象移除数组中指定的一组字符的所有尾部匹配项
        /// </summary>
        /// <param name="obj">转换为string进行TrimEnd操作的对象</param>
        /// <param name="trimChars">要删除的 Unicode 字符的数组，或 null</param>
        /// <returns>从当前字符串的结尾移除所出现的所有 trimChars 参数中的字符后剩余的字符串。如果 trimChars 为 null 或空数组，则改为删除 Unicode空白字符</returns>
        public static string TrimEnd(object obj, params char[] trimChars)
        {
            if (obj == null)
            {
                return string.Empty;
            }

            if (obj is string)
            {
                return ((string)obj).TrimEnd(trimChars);
            }
            else
            {
                return obj.ToString().TrimEnd(trimChars);
            }
        }



        #region Join扩展

        /// <summary>
        /// 拼接字符串
        /// </summary>
        /// <typeparam name="T">数据元素类型</typeparam>
        /// <param name="separator">分隔符</param>
        /// <param name="source">集合源</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素介安徽</param>
        /// <returns>结果字符串</returns>
        public static string Join<T>(char separator, IEnumerable<T> source, int startIndex, int count)
        {
            StringBuilder sb = new StringBuilder();
            int end = startIndex + count;
            for (int i = startIndex; i < end; i++)
            {
                if (sb.Length > 0)
                {
                    sb.Append(separator);
                }
                sb.Append(source.ElementAt(i));
            }
            return sb.ToString();
        }

        /// <summary>
        /// 拼接字符串
        /// </summary>
        /// <typeparam name="T">数据元素类型</typeparam>
        /// <param name="separator">分隔符</param>
        /// <param name="source">集合源</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素介安徽</param>
        /// <param name="selector">数据选择器</param>
        /// <returns>结果字符串</returns>
        public static string Join<T>(char separator, IEnumerable<T> source, int startIndex, int count, Func<T, string> selector)
        {
            StringBuilder sb = new StringBuilder();
            int end = startIndex + count;
            for (int i = startIndex; i < end; i++)
            {
                if (sb.Length > 0)
                {
                    sb.Append(separator);
                }
                sb.Append(selector(source.ElementAt(i)));
            }
            return sb.ToString();
        }

        /// <summary>
        /// 拼接字符串
        /// </summary>
        /// <typeparam name="T">数据元素类型</typeparam>
        /// <param name="separator">分隔符</param>
        /// <param name="source">集合源</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素介安徽</param>
        /// <returns>结果字符串</returns>
        public static string Join<T>(string separator, IEnumerable<T> source, int startIndex, int count)
        {
            StringBuilder sb = new StringBuilder();
            int end = startIndex + count;
            for (int i = startIndex; i < end; i++)
            {
                if (sb.Length > 0)
                {
                    sb.Append(separator);
                }
                sb.Append(source.ElementAt(i));
            }
            return sb.ToString();
        }

        /// <summary>
        /// 拼接字符串
        /// </summary>
        /// <typeparam name="T">数据元素类型</typeparam>
        /// <param name="separator">分隔符</param>
        /// <param name="source">集合源</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">元素介安徽</param>
        /// <param name="selector">数据选择器</param>
        /// <returns>结果字符串</returns>
        public static string Join<T>(string separator, IEnumerable<T> source, int startIndex, int count, Func<T, string> selector)
        {
            StringBuilder sb = new StringBuilder();
            int end = startIndex + count;
            for (int i = startIndex; i < end; i++)
            {
                if (sb.Length > 0)
                {
                    sb.Append(separator);
                }
                sb.Append(selector(source.ElementAt(i)));
            }
            return sb.ToString();
        }

        #endregion

    }
}
